package com.huawei_jikao;

import java.util.*;

/**
 * Created by Z-hong on 2024/7/11.
 */

public class HJ2 {
    static class TreeNode{
        int val;
        List<TreeNode> child;
        public TreeNode(int val){
            this.val = val;
            this.child = new ArrayList<>();
        }

    }

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        int num=in.nextInt();
        TreeNode [] arr = new TreeNode[num];
        for(int i=0;i<num;i++){
            int val=in.nextInt();
            arr[i] = new TreeNode(val);
        }
        for (int i = 0; i < num-1; i++) {
            int father=in.nextInt();
            int child=in.nextInt();
            arr[father-1].child.add(arr[child-1]);
        }
        int Max=0;
        for (int i = 0; i < num; i++) {
            Max= Math.max(Max,getCount(arr[i]));
        }
        System.out.println(Max);
    }

    public static int getCount(TreeNode treeNode){
        int count=treeNode.val;
        for (TreeNode child : treeNode.child) {
            count+= child.val;
        }
        return count;
    }
}
